angular.module('zqh.service', [])
    .factory('Storage', function () {
        return {
            set: function (key, data) {
                return window.localStorage.setItem(key, window.JSON.stringify(data));
            },
            get: function (key) {

                return window.JSON.parse(window.localStorage.getItem(key));
            },
            remove: function (key) {
                return window.localStorage.removeItem(key);
            }
        };
    })
    //用户登录
    .factory('Customer', function ($resource, $rootScope, $ionicLoading, API, Storage) {
        /*alert("点击登录");*/
        var ApiUrl = API.api,
            user,
            service = {};
        var resource = $resource(ApiUrl, {}, {
            query: {
                method: 'post',
                url: ApiUrl + 'Home/UserCenter/SignIn',
                timeout: 200
            }
        });

        service.checkLogin = function (mobile, password) {
            var match_mobile = /^1[345678]\d{9}$/;
            if (!match_mobile.test(mobile)) {
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "亲爱的，请输入正确的手机号",
                    duration: 1500
                });
                return false;
            }
            if (password.length < 6 || password.length > 18) {
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "亲爱的，密码长度为8~18位哦",
                    duration: 1500
                });
                return false;
            }
            return true;
        }

        service.login = function (mobile, password) {
            var md5_pass = hex_md5(password).toLowerCase();
            var sha1_pass = hex_sha1(md5_pass);
            var pay_pass = sha1_pass.split("").reverse().join("").toUpperCase();
            var post_data = {
                'mobile': mobile,
                'password': pay_pass,
                'device_id': '',
                'push_id': '',
                'no_need_sign': 'true'
            };
            return $.post(ApiUrl + 'Home/UserCenter/SignIn', post_data, function (response) {
                var res_data = eval(response);
                user = res_data;
                // var storageKey='errorMsg'
                // Storage.set(storageKey,res_data.data['customer']['real_name']);
                // $rootScope.$broadcast('Customer.loginUpdated');
            });

            // return resource.query({
            //     mobile: mobile,
            //     password: pay_pass,
            //     device_id:'867324021146036',
            //     push_id:'160a3797c8006d11290',
            //     sign: 'e2339dd7e6f01c6b8d4878a7fe0eebf3'
            // }, function(r) {
            //     user = r;
            //     console.log(r);
            //     $rootScope.$broadcast('Customer.loginUpdated');
            // },function (p) {
            //     var storageKey='errorMsg'
            //     Storage.set(storageKey,JSON.stringify(p));
            //     $rootScope.$broadcast('Customer.loginUpdated');
            // });
        }

        service.getCurrentUser = function () {
            return user;
        }

        return service;
    })

    .factory('ErrorFactroy', function ($resource, $rootScope, $ionicLoading, API) {
        var service = {};

        return service;
    })

    .factory('FinderFactroy', function ($resource, $rootScope, $ionicLoading, API, cfpLoadingBar) {
        var ApiUrl = API.api
        var service = {};
        var daoliudata;

        service.getData = function () {
            cfpLoadingBar.start();
            var postData = {};
            return $.post(ApiUrl + "ChannelManage/getChannelManage", postData, function (response) {
                cfpLoadingBar.complete();
                var res_data = eval(response);

                console.log(res_data);
                daoliudata = res_data;
                // daoliudata=response;
                // console.log(daoliudata);
                $rootScope.$broadcast('FinderFactroy.finder');
            })
        }

        service.getDaoliuData = function () {
            return daoliudata;
        }
        return service;
    })
